import axios from 'axios'
axios.defaults.timeout = 20000
axios.defaults.baseURL = ''
function startLoading () {
  // Indicator.open()
    console.log('加载中...')
}
function endLoading () {
  // Indicator.close()
    console.log('加载完成...')
}
let needLoadingRequestCount = 0

export function showFullScreenLoading () {
  if (needLoadingRequestCount === 0) {
    startLoading()
  }
  needLoadingRequestCount++
}

export function tryHideFullScreenLoading () {
  if (needLoadingRequestCount <= 0) return
  needLoadingRequestCount--
  if (needLoadingRequestCount === 0) {
    endLoading()
  }
}
axios.defaults.headers.post['Content-Type'] = 'application/json';

let instance = axios.create({
    headers: {'content-type':'multipart/form-data'}
});

// http request 拦截器
axios.interceptors.request.use(
  config => {
    config.data = JSON.stringify(config.data)
    if (config.showLoading) {
      showFullScreenLoading()
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)
// http response 拦截器
axios.interceptors.response.use(
  response => {
    if (response.config.showLoading) {
      tryHideFullScreenLoading()
    }
    // console.log(response.data)
    if (response.status !== 200 && response.status !== 301) {
      if (response.config.showLoading) alert(response.body.msg || '请求错误！')
      // response.request.onerror()
    }
    // for (let i = 0; i < response.cookie; i++) {
    //   console.log(response.cookie[i])
    // }
    if (response.data.cookie && response.data.cookie.length > 0) {
      console.log(response.data.cookie)
      // response.headers['set-cookie'] = response.data.cookie[0]
      document.cookie = response.data.cookie[0]
    }
    return response
  },
  error => {
    tryHideFullScreenLoading()
    if (error.config.showLoading) alert('请求错误！')
    return Promise.reject(error)
  }
)

/**
 * 封装get方法
 * @param url
 * @param params
 * @param showLoading
 * @returns {Promise}
 */
export function fetch (url, params = {}, showLoading = true) {
  // debugger
  return new Promise((resolve, reject) => {
    axios.get(url, {
      params: params,
      showLoading: showLoading,
      withCredentials: true
    })
      .then(response => {
        // console.log(response.data)
        resolve(response.data)
      })
      .catch(err => {
        reject(err)
      })
  })
}

/**
 * 封装post请求
 * @param url
 * @param data
 * @param config
 * @returns {Promise}
 */
export function post (url, data = {}, config = { showLoading: true }) {
  return new Promise((resolve, reject) => {
      console.log(config);
      axios.post(url, data)
      .then(response => {
        resolve(response.data)
      }, err => {
        reject(err)
      })
  })
}

/**
 * 封装post请求 FormData方式
 * @param url
 * @param data
 * @returns {Promise}
 */

export function postForm(url,data = {}){
    return new Promise((resolve,reject) => {
        instance.post(url,data)
            .then(response => {
                resolve(response.data);
            },err => {
                reject(err)
            })
    })
}

/**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function patch (url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.patch(url, data)
      .then(response => {
        resolve(response.data)
      }, err => {
        reject(err)
      })
  })
}

/**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function put (url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.put(url, data)
      .then(response => {
        resolve(response.data)
      }, err => {
        reject(err)
      })
  })
}
